package com.hsd.app.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hsd.app.model.HsdDssinfo;
import com.hsd.app.model.HsdMdInfo;
import com.hsd.app.service.IDataManageService;
import com.hsd.commons.model.HsdAccount;
import com.hsd.commons.model.HsdOrg;
import com.hsd.core.page.entity.PageParams;
import com.hsd.core.page.entity.Pagination;
import com.hsd.core.utils.TimeUtil;


@Controller
@RequestMapping("/datamanage")
public class MdManageAction extends PLBaseAction{
	@Autowired
	private IDataManageService hpdataService;
	
	
	/**
	 * 数据管理 ：加载穴位经络信息列表
	 * @param pageParams
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/loadMdList.do")
	public String loadMdList(PageParams pageParams,HttpServletRequest request,ModelMap model){
		String mdname = request.getParameter("mdname"); 
		Pagination page = hpdataService.loadMdList(pageParams,mdname);
		
		model.addAttribute("page",page);
		model.addAttribute("mdname",mdname); 
		return "app/data/data-md-list";
	}
	/**
	 * 新增一个穴位经络
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/addOneMd.do",produces = {"application/json;charset=UTF-8"})
	public @ResponseBody String addOneMd(HsdMdInfo mdobj,HttpServletRequest request,ModelMap model){
		Map query = new HashMap();
		query.put("mdName", mdobj.getMdName());
		Object existData = hpdataService.getUniqueObject(HsdMdInfo.class, query);
		if(null != existData){
			return "该名称已存在！";
		}
		mdobj.setId(this.hpdataService.getSeqNextVal("seq_hsd_md_info_id"));
		HsdAccount accInfo = this.getCurrentuser(request); 
		mdobj.setYouxiaobj(PLCONSTANTS.YOUXIAOBJ_YX);
		mdobj.setOpertime(TimeUtil.getCurrentDate());
		if(null != accInfo){
			mdobj.setUserid(accInfo.getId());
			mdobj.setUsername(accInfo.getUsername());
		} 
		hpdataService.save(mdobj); 
		 
		return "保存成功！";
	}
	/**
	 * 修改一个穴位经络
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/editOneMd.do",produces = {"application/json;charset=UTF-8"})
	public @ResponseBody String editOneMd(HsdMdInfo mdobj,HttpServletRequest request,ModelMap model){ 
		hpdataService.save(mdobj);
		return "修改成功！";
	}
	/**
	 * 查询一个穴位经络信息
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/selectOneMd.do")
	public  String  selectOneMd(HttpServletRequest request,ModelMap model){ 
		String id = request.getParameter("id");
		HsdMdInfo mdinfo = (HsdMdInfo)hpdataService.findByID(HsdMdInfo.class, new Long(id));
		model.put("mdinfo", mdinfo);
		return "app/data/edit-one-md";
	}
	
	/**
	 * 删除穴位经络
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/delOneMd.do",produces = {"application/json;charset=UTF-8"})
	public @ResponseBody String delOneMd(HttpServletRequest request,ModelMap model){
		String id = request.getParameter("id"); 
		HsdMdInfo mdobj = (HsdMdInfo)hpdataService.findByID(HsdMdInfo.class, new Long(id));
		mdobj.setYouxiaobj(PLCONSTANTS.YOUXIAOBJ_WX);
		hpdataService.saveObject(mdobj);
		return "删除成功！";
	}
}
